<?php

class FormElements
{
  private $erros;
  private $post;
    
  function FormElements()
  {
    $this->errors = array();
  }   
    
  function setError($fieldId,$error)
  {
    $this->errors[$fieldId] = $error;
  }
  
  function areThereErrors()
  {
    return sizeof($this->errors);
  }
    
  static function setPost($post)
  {
    $this->post = $post;
  }
  
  function getFormErrors() {
      $str = '{';
      foreach($this->errors as $id=>$msg){
      	$str .= "'$id':$msg,";
      }
      return (substr($str, 0 ,-1).'}');
   }

   static function row($label,$field,$class="") {
      return "<p class=\"$class\"><span>$label</span>$field</p>\n";
   }
   
   static function row2($label,$field,$class="") {
      return "<p class=\"$class\">$label</p>\n<p>$field</p>";
   }

   static function makeHiddenInput($name,$value) {
      return '<input id="'.$name.'" name="'.$name.'" type="hidden" value="'.$value.'" />'."\n";
   }
   
   function makeInput($name,$value="",$type="text",$class="") {
      
      $str = '<input ';
      if ($type=='checkbox' && isset($this->post[$name])) $str.='checked ';
      $str .='id="'.$name.'" name="'.$name.'" value="'.($value==''?($type=='password'?'':stripslashes($this->post[$name])):$value).'" class="'.$class.'"  type="'.$type.'" size="35"/>';
               
      return $str;
   }

   function makeTextarea($name,$value="",$class="", $rows=0,$cols=0) {
      return '<textarea id="'.$name.'" name="'.$name.'" class="'.$class.($rows && $cols?"rows=\"$rows\" cols=\"$cols\"":'').'>'.($value==''?stripslashes($this->post[$name]):$value).'</textarea>'."\n";
   }

  /*
   function makeFileInput($name,$value,$label,$class="input_class file_input",$labelClass="default_label") {
     //errors are output by makeInput() method
     //$tmp=$this->makeHiddenInput("MAX_FILE_SIZE","250000");
     $tmp = $this->makeInput($name,"",$label,"file",$class,$labelClass);

     if($value!="") {
        //$tmp.='<br /><div class="'.$labelClass.'">'.$vPage->tr('aktualni_soubor').'</div><span>'.str_replace(IMG_PATH,"",$value).'</span><br />'.N;
        //$tmp.='<div class="'.$labelClass.'">'.$vPage->tr('smazat_soubor').'</div> <input type="checkbox" class="delete_file_checkbox" value="'.$value.'" name="delete_'.$name.'" /><br />'.N;
     };
     return $tmp;
  }


   function makeSelect($name,$options=array(),$label,$class="select_class",$labelClass="default_label") {
       $tmp=$this->_errOut($name);
       $tmp.=$this->label($label,$labelClass,$this->_errLabelImpClass($name));
       $tmp.='<select name="'.$name.'" class="'.$class.$this->_errInputImpClass($name).'">'.N;
       $tmp.=join(N,$options);
       $tmp.='</select>'.N;
       return $tmp;
   }

   function makeOptions(&$optionsArray=array(),$value) {
       $tmp=array();
       foreach($optionsArray as $ind=>$text) {
          $tmp[]='<option value="'.$ind.'" '.($ind==$value? 'selected class="selected"' : '').'>'.$text.'</option>'.N;
       };
       return $tmp;
    }
    */
    /* select sthing as id, sthing as text 
    function makeOptionsFromSelect($sql,$value='') {
       $db=MySQL::getInstance();

       $res=$db->query($sql);
       $optArray=array();
       while($r=$db->fetchObject($res)) {
          $optArray[$r->id]=$r->text;
       };
       return $this->makeOptions($optArray,$hodnota);
    }



    */
    //$submitTypes=array(submutTypeId=>SubmitLabel)
    function makeSubmits($submitTypes) {
       $tmp="";
       foreach($submitTypes as $type=>$label) {
          $tmp.='<input class="submit" type="submit" name="submit['.$type.']" value="'.$label.'" />';
       }
       return $tmp;
    }
    
    function makeSubmit($name, $label, $class='submit') {
      return '<input id="'.$name.'" class="'.$class.'" type="submit" name="'.$name.'" value="'.$label.'" />'."\n";

    }
}

?>
